<template>
    <div class="app-container">
        <div class="filter-container">
            <el-input v-model="listQuery.name" placeholder="名字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
            <appSelect v-model="listQuery.appId" placeholder="所属应用"></appSelect>
            <companySelect v-model="listQuery.company" placeholder="所属商户"></companySelect>
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
                查询
            </el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
                新增
            </el-button>
        </div>

        <el-table
            :key="tableKey"
            v-loading="listLoading"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%;"
            @sort-change="sortChange"
        >

                    <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
                <template slot-scope="{row}">
                    <span>{{ row.id }}</span>
                </template>
            </el-table-column>
            <el-table-column label="设置项" width="380px" align="center">
                <template slot-scope="{row}" style="">
                      <div class="rowColl">秒杀订单超时时间(分) :<span class="span-left">{{ row.flashOrderOvertime }}</span></div>
                      <div class="rowColl">正常订单超时时间(分) :<span class="span-left">{{ row.normalOrderOvertime }}</span></div>
                      <div class="rowColl">发货后自动确认收货时间(天) :<span class="span-left">{{ row.confirmOvertime }}</span></div>
                      <div class="rowColl">自动完成交易时间,不能申请售后(天) :<span class="span-left">{{ row.finishOvertime }}</span></div>
                      <div class="rowColl">订单完成后自动好评时间(天) :<span class="span-left">{{ row.commentOvertime }}</span></div>
                </template>
            </el-table-column>
            <el-table-column label="所属应用" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.appName }}</span>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.createTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="更新时间" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.updateTime }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width" fixed="right">
                <template slot-scope="{row,$index}">
                    <el-button type="primary" size="mini" @click="handleUpdate(row)">
                        编辑
                    </el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(row,$index)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
            <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="110px" style="margin-left:50px;">
                <el-form-item label="秒杀订单超过：" prop="flashOrderOvertime">
                  <el-input v-model="temp.flashOrderOvertime" class="input-width">
                    <template slot="append">分</template>
                  </el-input>
                  <span class="note-margin">未付款，订单自动关闭</span>
                </el-form-item>
                <el-form-item label="正常订单超过：" prop="normalOrderOvertime">
                  <el-input v-model="temp.normalOrderOvertime" class="input-width">
                    <template slot="append">分</template>
                  </el-input>
                  <span class="note-margin">未付款，订单自动关闭</span>
                </el-form-item>
                <el-form-item label="发货超过：" prop="confirmOvertime">
                  <el-input v-model="temp.confirmOvertime" class="input-width">
                    <template slot="append">天</template>
                  </el-input>
                  <span class="note-margin">未收货，订单自动完成</span>
                </el-form-item>
                <el-form-item label="订单完成超过：" prop="finishOvertime">
                  <el-input v-model="temp.finishOvertime" class="input-width">
                    <template slot="append">天</template>
                  </el-input>
                  <span class="note-margin">自动结束交易，不能申请售后</span>
                </el-form-item>
                <el-form-item label="订单完成超过：" prop="commentOvertime">
                  <el-input v-model="temp.commentOvertime" class="input-width">
                    <template slot="append">天</template>
                  </el-input>
                  <span class="note-margin">自动五星好评</span>
                </el-form-item>
                <el-form-item label="所属应用">
                  <app-select v-model="temp.appId" />
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">
                    取消
                </el-button>
                <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
                    确定
                </el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    import {
        orderSettingList,
        orderSettingCreate,
        orderSettingUpdate,
        orderSettingDelete
    } from '@/api/orderSetting'
    import waves from '@/directive/waves'
    import Pagination from '@/components/Pagination'
    import appSelect from '@/views/app/components/appSelect'
    import companySelect from '@/views/app/components/companySelect'

    const defaultListQuery = {
        page: 1,
        limit: 15,
        name: '',
        company: '',
        appId:'',
        sort: '+id'
    };

    const currentTemp = {
        id: undefined,
        flashOrderOvertime: undefined,
        normalOrderOvertime: undefined,
        confirmOvertime: undefined,
        finishOvertime: undefined,
        commentOvertime: undefined,
        company: undefined,
        appId: undefined,
        createTime: undefined,
        updateTime: undefined,
    }

    export default {
        name: 'orderSetting',
        components: { Pagination, appSelect, companySelect },
        directives: { waves },
        filters: {

        },
        data() {
            // var name = (rule, value, callback) => {
            //   if (value === '') {
            //     callback(new Error('模拟后台校验，请输入用户名'));
            //   } else {
            //     callback();
            //   }
            // }
            return {
                tableKey: 0,
                list: null,
                total: 0,
                listLoading: true,
                listQuery: Object.assign({}, defaultListQuery),
                temp: Object.assign({}, currentTemp),
                dialogFormVisible: false,
                dialogStatus: '',
                textMap: {
                    update: '修改',
                    create: '新增'
                },
                rules: {
                    // name: [{ validator: name, trigger: 'blur' }]
                }
            }
        },
        created() {
            this.getList()
        },
        methods: {
            getList() {
                this.listLoading = true
                orderSettingList(this.listQuery).then(response => {
                    this.list = response.data.content
                    this.total = parseInt(response.data.totalElements)

                    this.listLoading = false
                })
            },
            handleFilter() {
                this.listQuery.page = 1
                this.getList()
            },
            sortChange(data) {
                const { prop, order } = data
                if (prop === 'id') {
                    this.sortByID(order)
                }
            },
            sortByID(order) {
                if (order === 'ascending') {
                    this.listQuery.sort = '+id'
                } else {
                    this.listQuery.sort = '-id'
                }
                this.handleFilter()
            },
            resetTemp() {
                this.temp = Object.assign({}, currentTemp)
            },
            handleCreate() {
                this.resetTemp()
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                })

            },
            createData() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        orderSettingCreate(this.temp).then(() => {
                            this.list.unshift(this.temp)
                            this.dialogFormVisible = false
                            this.$notify({
                                title: '提示',
                                message: '操作成功',
                                type: 'success',
                                duration: 2000
                            })
                            this.getList()
                        })
                    }
                })
            },
            handleUpdate(row) {
                this.temp = Object.assign({}, row) // copy obj
                this.dialogStatus = 'update'
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                })

            },
            updateData() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        const tempData = Object.assign({}, this.temp)
                        tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
                        orderSettingUpdate(tempData).then(() => {
                            const index = this.list.findIndex(v => v.id === this.temp.id)
                            this.list.splice(index, 1, this.temp)
                            this.dialogFormVisible = false
                            this.$notify({
                                title: '提示',
                                message: '操作成功',
                                type: 'success',
                                duration: 2000
                            })
                            this.getList()
                        })
                    }
                })
            },
            handleDelete(row, index) {
                const tempData = {};
                tempData.id = row.id;
                const that = this;
                this.$confirm('确认删除？', '系统提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    cancelButtonClass: 'btn-custom-cancel',
                    type: 'warning'
                }).then(function(){
                    orderSettingDelete(tempData).then(() => {
                        that.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        })
                        that.getList()
                    })
                }).catch(function(){
                    // console.log("取消成功！");
                })
            }
        }
    }
</script>

<style lang="scss" scoped>

  .input-width {
    width: 200px;
  }
  .note-margin {
    margin: 0 0 0 20px;
  }
  .rowColl {
    justify-content: left;
    display: flex;
    margin: 5px 0 5px 40px;
    .span-left {
      margin-left: 5px;
    }
  }

</style>
